import GameEvent from "../../Commons/GameEvent";
import { CardIdEnum } from "../../Enums/GameEnum";
import { getZeroTime } from "../../GlobalFunction";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import PlatformManager from "../../Platforms/PlatformManager";
import CardEntity from "./CardEntity";

export enum CardEvent {
    CARDBUY_EVENT = "cardBuy_event",
    CARDRECEIVE_EVENT = "cardReceive_event"
}

export default class CardManager implements IModelManager {
    private static _single: CardManager = null;
    public static get single(): CardManager {
        if (CardManager._single == null) {
            CardManager._single = new CardManager();
        }
        return CardManager._single;
    }

    initConfig(): void {

    }
    isHavePerpetualCard(): boolean {
        var usedata = SaveManager.single.userdata;
        return usedata.perpetualCard != null;
    }

    isHaveMonthCard(): boolean {
        var usedata = SaveManager.single.userdata;
        return usedata.monthCard != null && !usedata.monthCard.isOutDate;
    }

    async requestBuyCard(cardId: CardIdEnum) {
        var userdata = SaveManager.single.userdata;
        if (cardId == CardIdEnum.monthId) {
            userdata.monthCard = new CardEntity();
            userdata.monthCard.id = cardId;
            userdata.monthCard.isReceive = false;
            userdata.monthCard.receiveTime = getZeroTime();
            userdata.monthCard.endTime = cc.sys.now() + 30 * 24 * 60 * 60 * 1000;
            userdata.diamond += 666;
            UIManager.single.showTips("购买月卡成功， 钻石+666。");
            var accountName = cc.sys.localStorage.getItem("accountName");
            let result = await PlatformManager.single.basePlatform.sdk.payAmount(accountName, 28);
        } else if (cardId == CardIdEnum.perpetualId) {
            userdata.perpetualCard = new CardEntity();
            userdata.perpetualCard.id = cardId;
            userdata.perpetualCard.isReceive = false;
            userdata.perpetualCard.endTime = cc.sys.now() + 100 * 365 * 24 * 60 * 60 * 1000;
            userdata.perpetualCard.receiveTime = getZeroTime();
            userdata.diamond += 1888;
            UIManager.single.showTips("购买终身卡成功， 钻石+1888。");
            let result = await PlatformManager.single.basePlatform.sdk.payAmount(accountName, 88);
        }

        SaveManager.single.saveData();
        GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
        GameEvent.eventNode.emit(CardEvent.CARDBUY_EVENT);

    }

    async requestReceive(cardId: CardIdEnum) {
        var userdata = SaveManager.single.userdata;
        if (cardId == CardIdEnum.monthId) {
            userdata.monthCard.isReceive = true;
            userdata.diamond += 100;
            UIManager.single.showTips("钻石 +100。");
        } else if (cardId == CardIdEnum.perpetualId) {
            userdata.perpetualCard.isReceive = true;
            userdata.diamond += 300;
            UIManager.single.showTips("钻石 +300。");
        }

        SaveManager.single.saveData();
        GameEvent.eventNode.emit(CardEvent.CARDRECEIVE_EVENT);
        GameEvent.eventNode.emit(GameEvent.CURRENCYUPDATA_EVENT);
    }


}
